package com.yb.sys.user.vo;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/11/23 16:03
 * FileName: PageRequest
 * Description:
 */


public class PageReq extends PageRequest {
    private int page=DEFAULT_PAGE;
    private int size=DEFAULT_SIZE;

    private Sort sort=Sort.unsorted();

    private Map<String,Sort.Direction> sorts;
    private static final int DEFAULT_PAGE=1;
    private static final int DEFAULT_SIZE=10;

    /**
     * Creates a new {@link PageReq} with sort parameters applied.
     *
     * @param page one-based page index, must not be negative or zero.
     * @param size the size of the page to be returned, must be greater than 0.
     * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead.
     */
    public PageReq(int page, int size, Sort sort) {
        super(page, size, sort);
        this.page=page;
        this.size=size;
        this.sort=sort;
    }

    public PageReq(){
        super(DEFAULT_PAGE,DEFAULT_SIZE,Sort.unsorted());
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        //page大于0
        if(page>0)
          this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    @Override
    public int getPageNumber() {
        return page;
    }

    @Override
    public int getPageSize() {
        return size;
    }

    @Override
    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }


    public void setSorts(Map<String,Sort.Direction> sortItems) {

        System.out.println(sortItems);
        if(sortItems!=null&&sortItems.size()>0){
            List<Sort.Order> orderList=new ArrayList<>();
            sortItems.forEach((key,value)->{
                orderList.add(new Sort.Order(value,key));
            });
            this.sort=Sort.by(orderList);
        }

        this.sorts = sortItems;

    }

    public Map<String,Sort.Direction> getSorts() {
        return sorts;
    }
}
